Chapter 4 MAG catalogue

load("data/data.Rdata")

4.1 filter samples with high host data

sample_metadata <- sample_metadata%>%
  filter(!sample %in% c("EHI02721", "EHI02712", "EHI02700", "EHI02720", "EHI02749", "EHI02719", "EHI02729", "EHI02715", "EHI02722"))

genome_counts_filt <- genome_counts %>% 
    select(one_of(c("genome",sample_metadata$sample)))%>%
  filter(rowSums(. != 0, na.rm = TRUE) > 0) %>%
  select_if(~!all(. == 0))

genome_metadata <- genome_metadata %>% 
  semi_join(., genome_counts_filt, by = "genome") %>% 
  arrange(match(genome,genome_counts_filt$genome))

genome_tree <- keep.tip(genome_tree, tip=genome_metadata$genome) # keep only MAG tips

#load("data/genome_gifts.Rdata")

4.2 Genome phylogeny

# Generate the phylum color heatmap
phylum_heatmap <- read_tsv("https://raw.githubusercontent.com/earthhologenome/EHI_taxonomy_colour/main/ehi_phylum_colors.tsv") %>%
    right_join(genome_metadata, by=join_by(phylum == phylum)) %>%
    arrange(match(genome, genome_tree$tip.label)) %>%
    select(genome,phylum) %>%
    mutate(phylum = factor(phylum, levels = unique(phylum))) %>%
    column_to_rownames(var = "genome")

# Generate  basal tree
circular_tree <- force.ultrametric(genome_tree, method="extend") %>% # extend to ultrametric for the sake of visualisation
    ggtree(., layout="fan", open.angle=10, size=0.5)
***************************************************************
*                          Note:                              *
*    force.ultrametric does not include a formal method to    *
*    ultrametricize a tree & should only be used to coerce    *
*   a phylogeny that fails is.ultrametric due to rounding --  *
*    not as a substitute for formal rate-smoothing methods.   *
***************************************************************
# Add phylum ring
circular_tree <- gheatmap(circular_tree, phylum_heatmap, offset=0.55, width=0.1, colnames=FALSE) +
        scale_fill_manual(values=phylum_colors) +
        geom_tiplab2(size=1, hjust=-0.1) +
        theme(legend.position = "none", plot.margin = margin(0, 0, 0, 0), panel.margin = margin(0, 0, 0, 0))

# Flush color scale to enable a new color scheme in the next ring
circular_tree <- circular_tree + new_scale_fill()

# Add completeness ring
circular_tree <- circular_tree +
        new_scale_fill() +
        scale_fill_gradient(low = "#d1f4ba", high = "#f4baba") +
        geom_fruit(
                data=genome_metadata,
                geom=geom_bar,
                mapping = aes(x=completeness, y=genome, fill=contamination),
                offset = 0.55,
                orientation="y",
              stat="identity")

# Add genome-size ring
circular_tree <-  circular_tree +
        new_scale_fill() +
        scale_fill_manual(values = "#cccccc") +
        geom_fruit(
             data=genome_metadata,
             geom=geom_bar,
             mapping = aes(x=length, y=genome),
                 offset = 0.05,
                 orientation="y",
         stat="identity")

# Add text
circular_tree <-  circular_tree +
        annotate('text', x=3.6, y=0, label='            Phylum', family='arial', size=3.5) +
        annotate('text', x=4.7, y=0, label='                         Genome quality', family='arial', size=3.5) +
        annotate('text', x=5.3, y=0, label='                     Genome size', family='arial', size=3.5)

#Plot circular tree
circular_tree %>% open_tree(30) %>% rotate_tree(90)

4.3 Taxonomy overview

genome_metadata %>% 
  group_by(phylum) %>%
  summarise(mag_n=n()) %>%
  arrange(-mag_n) %>%
  tt()
tinytable_04a7djey13b6evig3cwg
phylum mag_n
p__Bacillota_A 526
p__Bacteroidota 127
p__Bacillota 53
p__Pseudomonadota 39
p__Verrucomicrobiota 35
p__Synergistota 18
p__Actinomycetota 15
p__Patescibacteria 13
p__Desulfobacterota 12
p__Bacillota_C 9
p__Cyanobacteriota 7
p__Bacillota_B 2
p__Spirochaetota 2
p__Campylobacterota 1
p__Methanobacteriota 1

4.4 Mag size (MB)

Mags average size (MB)

genome_metadata %>% 
  summarise(Average_size=mean(length))
# A tibble: 1 × 1
  Average_size
         <dbl>
1     2102306.

Minimum Mags size (MB)

genome_metadata %>% 
  filter(length==min(length))
# A tibble: 1 × 11
  genome           domain      phylum             class              order family genus species completeness contamination length
  <chr>            <chr>       <chr>              <chr>              <chr> <chr>  <chr> <chr>          <dbl>         <dbl>  <dbl>
1 EHA03306_bin.234 d__Bacteria p__Patescibacteria c__Saccharimonadia o__S… f__Na… g__N… s__             52.6          7.13 326026

Mags arrange by size (MB)

genome_metadata %>% 
  arrange(length)
# A tibble: 860 × 11
   genome           domain      phylum              class            order family genus species completeness contamination length
   <chr>            <chr>       <chr>               <chr>            <chr> <chr>  <chr> <chr>          <dbl>         <dbl>  <dbl>
 1 EHA03306_bin.234 d__Bacteria p__Patescibacteria  c__Saccharimona… o__S… f__Na… g__N… s__             52.6          7.13 326026
 2 EHA03306_bin.233 d__Bacteria p__Bacillota        c__Bacilli       o__R… f__UB… g__R… s__             58.8          0.71 393300
 3 EHA03305_bin.87  d__Bacteria p__Bacillota_A      c__Clostridia    o__C… f__CA… g__H… s__             58.1          0.58 569748
 4 EHA03337_bin.2   d__Bacteria p__Bacillota        c__Bacilli       o__R… f__UB… g__O… s__             61.8          2.13 593604
 5 EHA03305_bin.302 d__Bacteria p__Patescibacteria  c__Saccharimona… o__S… f__Na… g__N… s__             55.2          4.48 600188
 6 EHA03305_bin.141 d__Bacteria p__Patescibacteria  c__Saccharimona… o__S… f__Na… g__N… s__             86.4          1.22 616303
 7 EHA03306_bin.147 d__Bacteria p__Bacillota_A      c__Clostridia    o__T… f__CA… g__C… s__             52.8          6.44 621291
 8 EHA03290_bin.28  d__Bacteria p__Patescibacteria  c__Saccharimona… o__S… f__Na… g__N… s__             79.1          5.36 637327
 9 EHA04588_bin.191 d__Bacteria p__Campylobacterota c__Campylobacte… o__C… f__He… g__H… s__             58.3          1.22 637744
10 EHA03337_bin.34  d__Bacteria p__Bacillota_C      c__Negativicutes o__S… f__Se… g__   s__             51.6          0.1  652142
# ℹ 850 more rows

Mags average size and completeness by phylum (MB)

genome_metadata %>% 
  group_by(phylum) %>%
  summarise(average_size=mean(length),average_comp=mean(completeness))
# A tibble: 15 × 3
   phylum               average_size average_comp
   <chr>                       <dbl>        <dbl>
 1 p__Actinomycetota        1487860.         76.6
 2 p__Bacillota             1297997.         83.9
 3 p__Bacillota_A           2041433.         81.5
 4 p__Bacillota_B           1738690          90  
 5 p__Bacillota_C           1510294.         90.9
 6 p__Bacteroidota          3077607.         76.6
 7 p__Campylobacterota       637744          58.3
 8 p__Cyanobacteriota       1722146.         93.0
 9 p__Desulfobacterota      1892601.         85.5
10 p__Methanobacteriota     1705853          99.8
11 p__Patescibacteria        763324.         77.4
12 p__Pseudomonadota        2121766.         82.7
13 p__Spirochaetota         1944581          94.7
14 p__Synergistota          1619963.         75.8
15 p__Verrucomicrobiota     2066309.         81.2

4.5 Genome quality

genome_metadata %>% 
    summarise(completeness_mean=mean(completeness) %>% round(2) %>% as.character(), 
              completeness_sd=sd(completeness) %>% round(2) %>% as.character(), 
              contamination_mean=mean(contamination) %>% round(2), 
              contamination_sd=sd(contamination) %>% round(2)) %>%
    unite("Completeness",completeness_mean, completeness_sd, sep = " ± ", remove = TRUE) %>%
    unite("Contamination",contamination_mean, contamination_sd, sep = " ± ", remove = TRUE) %>%
    tt()
tinytable_dg1ivo1mj5mssey6na5x
Completeness Contamination
80.99 ± 16.79 2.21 ± 2.32
#Generate quality biplot
genome_biplot <- genome_metadata %>%
  select(c(genome,domain,phylum,completeness,contamination,length)) %>%
  arrange(match(genome, rev(genome_tree$tip.label))) %>% #sort MAGs according to phylogenetic tree
  ggplot(aes(x=completeness,y=contamination,size=length,color=phylum)) +
              geom_point(alpha=0.7) +
                    ylim(c(10,0)) +
                    scale_color_manual(values=phylum_colors) +
                    labs(y= "Contamination", x = "Completeness") +
                    theme_classic() +
                    theme(legend.position = "none")

#Generate contamination boxplot
genome_contamination <- genome_metadata %>%
            ggplot(aes(y=contamination)) +
                    ylim(c(10,0)) +
                    geom_boxplot(colour = "#999999", fill="#cccccc") +
                    theme_void() +
                    theme(legend.position = "none",
                        axis.title.x = element_blank(),
                        axis.title.y = element_blank(),
                        axis.text.y=element_blank(),
                        axis.ticks.y=element_blank(),
                        axis.text.x=element_blank(),
                        axis.ticks.x=element_blank(),
                        plot.margin = unit(c(0, 0, 0.40, 0),"inches")) #add bottom-margin (top, right, bottom, left)

#Generate completeness boxplot
genome_completeness <- genome_metadata %>%
        ggplot(aes(x=completeness)) +
                xlim(c(50,100)) +
                geom_boxplot(colour = "#999999", fill="#cccccc") +
                theme_void() +
                theme(legend.position = "none",
                    axis.title.x = element_blank(),
                    axis.title.y = element_blank(),
                    axis.text.y=element_blank(),
                    axis.ticks.y=element_blank(),
                    axis.text.x=element_blank(),
                    axis.ticks.x=element_blank(),
                    plot.margin = unit(c(0, 0, 0, 0.50),"inches")) #add left-margin (top, right, bottom, left)

#Render composite figure
grid.arrange(grobs = list(genome_completeness,genome_biplot,genome_contamination),
        layout_matrix = rbind(c(1,1,1,1,1,1,1,1,1,1,1,4),
                              c(2,2,2,2,2,2,2,2,2,2,2,3),
                              c(2,2,2,2,2,2,2,2,2,2,2,3),
                              c(2,2,2,2,2,2,2,2,2,2,2,3),
                              c(2,2,2,2,2,2,2,2,2,2,2,3),
                              c(2,2,2,2,2,2,2,2,2,2,2,3),
                              c(2,2,2,2,2,2,2,2,2,2,2,3),
                              c(2,2,2,2,2,2,2,2,2,2,2,3),
                              c(2,2,2,2,2,2,2,2,2,2,2,3),
                              c(2,2,2,2,2,2,2,2,2,2,2,3),
                              c(2,2,2,2,2,2,2,2,2,2,2,3),
                              c(2,2,2,2,2,2,2,2,2,2,2,3)))

4.6 Functional overview

# Aggregate basal GIFT into elements
genome_gifts<-genome_gifts_raw
function_table <- genome_gifts %>%
    to.elements(., GIFT_db)

# Generate  basal tree
function_tree <- force.ultrametric(genome_tree, method="extend") %>%
                ggtree(., size = 0.3) 
***************************************************************
*                          Note:                              *
*    force.ultrametric does not include a formal method to    *
*    ultrametricize a tree & should only be used to coerce    *
*   a phylogeny that fails is.ultrametric due to rounding --  *
*    not as a substitute for formal rate-smoothing methods.   *
***************************************************************
#Add phylum colors next to the tree tips
function_tree <- gheatmap(function_tree, phylum_heatmap, offset=0, width=0.1, colnames=FALSE) +
            scale_fill_manual(values=phylum_colors) +
            labs(fill="Phylum")

#Reset fill scale to use a different colour profile in the heatmap
function_tree <- function_tree + new_scale_fill()

#Add functions heatmap
function_tree <- gheatmap(function_tree, function_table, offset=0.5, width=3.5, colnames=FALSE) +
            vexpand(.08) +
            coord_cartesian(clip = "off") +
            scale_fill_gradient(low = "#f4f4f4", high = "steelblue", na.value="white") +
            labs(fill="GIFT")

#Reset fill scale to use a different colour profile in the heatmap
function_tree <- function_tree + new_scale_fill()

# Add completeness barplots
function_tree <- function_tree +
            geom_fruit(data=genome_metadata,
            geom=geom_bar,
            grid.params=list(axis="x", text.size=2, nbreak = 1),
            axis.params=list(vline=TRUE),
            mapping = aes(x=length, y=genome, fill=completeness),
                 offset = 3.8,
                 orientation="y",
                 stat="identity") +
            scale_fill_gradient(low = "#cf8888", high = "#a2cc87") +
            labs(fill="Genome\ncompleteness")

function_tree

4.7 Functional ordination

# Generate the tSNE ordination
tSNE_function <- Rtsne(X=function_table, dims = 2, check_duplicates = FALSE)

# Plot the ordination
function_ordination <- tSNE_function$Y %>%
                as.data.frame() %>%
                mutate(genome=rownames(function_table)) %>%
                inner_join(genome_metadata, by="genome") %>%
                rename(tSNE1="V1", tSNE2="V2") %>%
                select(genome,phylum,tSNE1,tSNE2, length) %>%
                ggplot(aes(x = tSNE1, y = tSNE2, color = phylum, size=length))+
                            geom_point(shape=16, alpha=0.7) +
                            scale_color_manual(values=phylum_colors) +
                            theme_minimal() +
                labs(color="Phylum", size="Genome size") +
                guides(color = guide_legend(override.aes = list(size = 5))) # enlarge Phylum dots in legend

function_ordination